package love.yuanmengrobot.als.business.dao.impl;/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:zjj
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/2 17:27
 */

import love.yuanmengrobot.als.utilPublic.DQL;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;
import love.yuanmengrobot.als.utilPublic.SqlString;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;


/*
 * 根据学生id获取学生每一学年修得学分
 * @return ArrayList 按顺序存放在数组中
 * @id 学生id*/
public class StudentCourseTermDaoImpl {
    ArrayList getCourseTermScore(String id){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        int year=2000+Integer.parseInt(id.substring(0,2));    //该学生入学年份
        int year1=getStudentYear();                           //当前学年
        HashMap<String, Float> stringFloatHashMap = new HashMap<String, Float>();
        ArrayList credits=new ArrayList<Float>();

        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
             rs = stmt.executeQuery(SqlString.selectCourseTermScore(id));
            while (rs.next()){
                stringFloatHashMap.put(rs.getString("COURSE_TERM"),rs.getFloat("CREDIT"));
            }


            for(int i=year;i<=year1;i++){                //统计每学年所修得学分
                float credit=0;
                int j=0;
                for (String key : stringFloatHashMap.keySet()){
                    if(j>=2){
                        break;
                    }
                    if(key.substring(0,4).equals(i+"")){
                        credit=credit+stringFloatHashMap.get(key);
                        j++;
                    }

                }
                credits.add(credit);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally{
            JdbcUtil.close(rs, stmt, conn);
        }

        return credits;
    }




    /*
     * 获取当前时间是哪一学年
     * @return int 当前时间是哪一学年*/
    static int  getStudentYear(){
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Date parse = df.parse(year + "-09-01");
            if(date.after(parse)||date.equals(parse)){
                return year;
            }
            if(date.before(parse)){
                return year-1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return year;
    }

}
